package com.epam.service.serviceImpl;

import com.epam.Entity.User;
import com.epam.repository.UserRepository;
import com.epam.service.MemberService;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private UserRepository userRepository;
    /**
     * 创建会员
     * @param user
     * @return
     */
    @Override
    public User createMember(User user){
        //使用h2DB 要求存储时必须指定主键
        List<User> ids = userRepository.findAll();
        user.setUserId(UUID.randomUUID().toString());
        user.setUserName(user.getUserName());
        user.setVipType(user.getVipType());
        user.setCreateTime(DateUtil.now());
        user.setModifyTime(DateUtil.now());
        User res = userRepository.saveAndFlush(user);
        System.out.print(res);
        if(res.getUserId() != null){
            return user;
        }
        return null;
    }

}
